Okay.
Today's lecture is going to dive deeper into the data type string.
We're going to learn how to add strings together, how to get specific parts of a string, how to use
escape characters and how to use the string library.
So let's get started.
Now we already know that a string is simply a collection of text, right?
We can create a string using quotations such as single quotations or double quotations.
And we can also create multi-line strings by using two brackets like this.
However, we can also manipulate strings in many different ways, and we can do so by using the string
library.
A library is simply a collection of pre-written code that developers can use to complete tasks without
having to implement the code yourself.
Specifically, the string library is built into Lua and lets us do several things like make a string
completely uppercase, lowercase, or split the string into multiple different sections based on a specific
character occurrence.
So let's go ahead and show this library in action.
I'm going to create a variable here.
And I'm just going to call this variable example string.
And we can create a string of like hello world.
And what I'm going to do is first call our print function and pass our example string.
And that means we should expect to see Hello World get printed in the output.
And then next what I'm going to do is I'm going to set example string.
I'm going to override the value inside of there equal to.
And then to access the string library we're going to type out string.
And then we can use the dot operator to index the string library.
And here we get to see all of the different functions available to us inside of this library.
For example we can set a string to be all lowercase.
We can set one to be all uppercase.
We can reverse a string.
We can get sub strings.
And there's a whole bunch of other different functions inside of the string library.
So what I'm going to do first is demonstrate the string dot lower function.
So we'll autofill that here.
And then we can just pass example string again.
So it's going to grab the value of hello World.
And it's going to make all of the characters in there lowercase.
And then we can call our print function again and pass our example string.
And we should expect Hello World to print here, but it should be all lowercase.
And then once more I'm going to override the value inside of example string.
And I'm going to set it equal to.
And this time we're going to use the upper function.
And then we can pass example string.
And then we'll print out that string as well.
So we should get all uppercase letters here.
And then I'm also going to demonstrate the reverse function.
So again we'll override example string and use the string dot reverse.
And then we can pass example string once more.
And then the last function I would like to demonstrate is the sub or sub string function.
So we'll do example string equal to string dot sub.
And what this does you can see the definition right here returns the sub string of s which is what we
pass up here that starts at I which is an index and continues until J which is the second index.
So what this means is we pass a string to it, and we basically grab a chunk out of that string and
store it inside of a variable.
Or that's what this function returns.
So for example, if I wanted to extract the word world only in here I could use the string sub function
pass where exactly the word world starts and ends.
So for example here at the first index which would be one is h, two is E and so on.
And then we get to W which would have its own index.
And then the last letter here D would also have its own index.
So what I'm going to do is I'm going to do string dot sub.
I'm going to pass my example string.
And I want to get a sub string of only world here.
So we can count.
This is one character 2345678.
So we know that the W starts at eight.
So we're going to pass eight here.
And then 910 1112 it ends at 12.
So we can pass 12 as the second argument.
And what this function is going to do is it's going to extract the string of world and store it inside
of our function right here.
And before we do that, I'm also going to print our previous example when we reverse our string here.
So now when we print we should get our default string here.
We should get it Lowercased.
Here we'll get it uppercase.
This will print it reversed.
And then last but not least this will store it as the word of world only.
And then we can also print that out into the console to see that as well.
And then one last thing I would like to show you is how to get the length of a string.
And it's actually very easy to do.
What we do is we first put a hashtag and then we can type out our string.
And what this returns is a number that represents the exact length of the string.
And we can go ahead and print this out in the console as well.
So now let's go ahead and head to the test tab.
Hit the drop down menu and press run.
And as you can see, we get all of our different strings printed inside of the console.
Here we get our regular string printed out.
Hello world!
Here we get it all completely lowercased.
The next one is all uppercase.
Here we have it reversed and then here.
Oh that's right, we reversed it.
So we actually didn't get world here.
But instead we got a comma o l l e.
And that's because we had the string reversed.
So it's grabbing these last indexes instead of world.
So if we actually wanted to grab world again we would have to reverse our string once more.
But as you can see right here, it printed out the number five, which represents the length or the
amount of characters inside of the string.
As you can see, we have one, two, three, four, and five, so that number perfectly represents the
length of the string.
So what I'm going to do here is before we grab the substring of our example string, I'm going to reverse
it once more.
So example string is equal to string dot reverse and pass example string.
And this should now store the word of world.
So we can go ahead and hit run again.
And there we go.
We get our word world.
It grabbed it from the eighth to the 11th index.
And that function is inclusive, meaning it includes the letter that was at the eighth index, which
was W, and it includes the letter that was at the last index, which is the letter D, and we still
get the length of five characters.
Perfect.
Next, I'm going to show you how you can concatenate two strings together.
What is concatenate?
Well, concatenate is basically just a fancy word for adding two things together.
If I have multiple strings, I would like to combine into a single string.
This is where we would use concatenation.
So what I'm going to do is I'm going to create a second string.
I'm going to just call this variable example string two.
And I'm going to store in there.
It's a great day.
And then I'm going to create a third variable.
I'm going to call this example string three.
And what I'm going to do is I'm going to reference my first example string, which by the time it reaches
this point it will just be the word world.
Because remember Lua executes code serially starting at the top and going down.
And that's perfectly demonstrated by each of our print statements, right?
We print out the string, we apply a change to the string, it prints that change out, and it keeps
going down line by line, as demonstrated here in the console.
So we'll get the word of world here.
And then to concatenate it with this second string right here we are going to use two dots.
So we do one and then two.
And then we get to pass a second string which is going to be example string two.
Now what this has done is it's joined these two strings together into a single string and stored it
inside of this variable.
That means we can go ahead and print out this variable inside of the console.
And let's go ahead and see what we get.
So now here's our two strings joined together.
It says world.
It's a great day.
Now you may be seeing.
Hey, there's no space between those two words.
How can we fix that?
Well, that's also very easy to do.
We'll go back to our script.
And what we can do is we're going to put a quotation mark here and then another one.
So this is a new string data type right here.
And then we're going to add another two dots.
And then inside of the string we're just going to put a space.
So now we are joining these three different strings together.
And hopefully now we should have a space between these two different strings.
So if we hit run again.
As you can see, we now have world.
It's a great day and there is a space between world.
And it's.
One last thing I would like to mention in this lecture are escape characters.
What exactly is an escape character?
Well, an escape character is a special character format that doesn't represent itself as a literal
string.
Instead, it performs a special action.
You might not know what I mean, but I'm going to demonstrate it to you here in a moment.
What I'm going to do is I'm going to create a new variable again, and I'm going to call this variable
escape characters.
And we're going to set it equal to a new string.
And we can say something like and Bob said today was a crazy day.
Now you might be wondering, how can we include quotations in our text when quotations denote a string
data type?
Right.
We're running into a problem here where we would like to include these quotation marks, but the interpreter
doesn't know what's going on.
Well, that's where an escape character would come into play.
For example, here, this escape character, which is backslash, allows us to include this quotation
mark into the string.
This escape character here has a very special action to omit whatever is in front of it and include
it into the string.
And I can do it the exact same way here as well.
So now this quotation mark here and this quotation mark here are going to be included inside of the
string.
This is the role of this particular escape character.
Another escape character is where we can print all of the text after it on a new line, and that is
the escape character of another backslash.
And then we pass n.
So what this is going to do is it's going to grab all of the text after it, and it's going to print
it on a new line, hence the n.
So we can go ahead and print out escape characters and you'll see exactly what I mean.
So if we hit run here.
We actually got an error attempt to call a nil value on line 28.
I always love errors because we get to solve them in real time.
So if we go to line 28, oh, it's because I accidentally misspelled print.
My bad.
Let's go ahead and try that again.
We'll hit run.
And there we go.
It says.
And Bob said, we get a new line created where this second part of the string is printed, and it says,
today was a crazy day.
And as you can see, it has included our quotation marks.
So that was the role of that first escape character, the single backslash, and then that other escape
character, the backslash n moved all of this other text in our string to a new line.
Another escape character I like to demonstrate to you is backslash t.
So what I'm going to do is I'm going to set escape characters equal to a new string.
And I'm just going to say example of and we can do backslash t escape character.
Now, if I would actually like to include backslash t inside of the text, what I'm going to have to
do here is I'm going to have to create another backslash t, but instead I'm going to put another slash
which is going to omit this original slash, which means these two characters right here are going to
be included inside of my string.
And then afterwards we have our escape character right here.
And what this is going to do is it's going to insert a horizontal space within our string.
So that means example of slash T and then escape character.
There's going to be a big gap between these two.
So let me print this out inside of the console and let me not misspell it again.
And then we can hit run.
And as you can see, we get an example of backslash t.
We get our gap and then escape character.
So as you can see it has inserted a horizontal spacing between these two strings.
Now, these were just some of the main tools at your disposal to manipulate strings.
You should now have a good understanding of the string library and text manipulation inside of your
scripts.
I will see you in the next lecture.